<?php
  class Home_m extends Model{
      function Home_m(){
          parent::Model();
          $this->load->database();
      }
      function process_login($name,$pass){
          $query = $this->db->get_where('users', array('name'=>$name,'pass'=>$pass));
          return $query;
      }
      function get_all_categories(){
      	 $query = $this->db->get_where('categories', array('parent'=>'0'));
     	 return $query->result_array();
      }
      
      	function translate($key){
      	$this->db->select('key,'.$key);
     	  $this->db->from('lang');
     	  $query=$this->db->get();
  	  $list=false;
     	   foreach($query->result_array() as $aRow):
		$list[$aRow['key']]=$aRow[$key];
       endforeach;
  	
  		  return $list;

  	}
  	//**********************NOTRE ENTREPRISE**************************
  	function get_content($key){
  		$this->db->select();
     	  $this->db->from('notre_ent');
     	  $this->db->where('key',$key);
     	  $query=$this->db->get();
     	 return $query->row_array();  
  	}
  	//**********************RECHERCHE******************************
  	function get_product_recherche($recherche,$per_page,$offset){
      	
      	 $this->db->select();
     	  $this->db->from('animaux');
     	   $this->db->like('nom',$recherche); 
     	  $this->db->limit($per_page,$offset);
     	 
     	  $query=$this->db->get();
     	    return $query->result_array();
  	}
  function count_product_recherche($recherche){
      	
      	 $this->db->select();
     	  $this->db->from('animaux');
     	 $this->db->like('nom',$recherche); 
     	 
     	  $query=$this->db->get();
     	    return $query->num_rows();
  	}
  	 function count_product_categories_recherche($recherche,$list){
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('animaux');
     	  $this->db->where('catid in '.$list);
     	  $this->db->like('nom',$recherche); 
     	  $query=$this->db->get();
     	    return $query->num_rows();
       }else return false;
  	}
  	
      function get_product_categories_recherche($recherche,$list,$per_page,$offset){
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('animaux');
     	   $this->db->where('catid in '.$list);
     	   $this->db->like('nom',$recherche); 
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
       }else return false;
  	}
      	//****************PRODUITS*************************************
    
      
       function get_all_id_categories_child($catid){
       	$list=false;
       	if ($catid!=0){
       		
       		$list['id']="(";
       		$query = $this->db->get_where('categories', array('parent'=>$catid));
              if ($query->num_rows()>0) 
	               { 
               	    foreach($query->result_array() as $aRow):
						$list['id'].=$aRow['id'].',';
                            endforeach;
                $list['id']= substr($list['id'],0,strlen($list['id'])-1).')';  
                $list['array']= $query->result_array();         
               	return $list;
           	}
			else{
				$array=array('id' => $catid, 'parent >' => 0); 
			$this->db->select();
			 $this->db->from('categories');
     	  $this->db->where($array);
     	  $query=$this->db->get();	
				 if ($query->num_rows()>0) 
	               { 
               	    foreach($query->result_array() as $aRow):
						$list['id'].=$aRow['id'].')';
                            endforeach;
                    $list['array']= $query->result_array();           
               	return $list;
           	 } else return false;
			}
		} else { return $list=false;}
      }
      function get_product($per_page,$offset,$dispo=0){
      	
      	 $this->db->select();
     	  $this->db->from('animaux');
     	  if($dispo==1) $this->db->where('prix >','0');
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
  	}
  	function count_product($dispo=0){
      	
      	 $this->db->select();
     	  $this->db->from('animaux');
     	  if($dispo==1) $this->db->where('prix >','0');
     	 
     	  $query=$this->db->get();
     	    return $query->num_rows();
  	}
  
  	 function count_product_categories($list,$dispo=0){
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('animaux');
     	  if($dispo==1) $this->db->where('prix >','0');
     	   $this->db->where('catid in '.$list);
     	  $query=$this->db->get();
     	    return $query->num_rows();
       }else return false;
  	}
  	
      function get_product_categories($list,$per_page,$offset,$dispo=0){
      	if($list!=false){
      		$this->db->select();
     	  $this->db->from('animaux');
     	 if($dispo==1) $this->db->where('prix >','0');
     	    $this->db->where('catid in '.$list);
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
       }else return false;
  	}
  	function get_img_categories($catid){
  		$query = $this->db->get_where('categories', array('id'=>$catid));
  		 return $query->row_array();
  	}
  	
  	function get_detail_product($id){
  		 $this->db->select('animaux.*,geo_countries.name as originename');
           $this->db->from('animaux');
           $this->db->join('geo_countries', 'geo_countries.id = animaux.origine');
          $this->db->where('animaux.id =',$id);
          
            $query=$this->db->get();
     	
  		  return $query->result_array();
  	}
  	
 
  	
  	
  	
  	
  	//****************ACTU*************************************
  	
  	 function get_all_categories_actu(){
  	 	$query=$this->db->get('catactu');
      	return $query->result_array();
      	
      }
      	function get_actu($per_page,$offset){
      		$this->db->select();
     	  $this->db->from('actu');
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	    return $query->result_array();
    
  	}
  		function count_actu_categories($id){
  		$this->db->select();
     	  $this->db->from('actu');
     	  $this->db->where('catid = ',$id);
     	  
     	  $query=$this->db->get();
     	  
    	 return $query->num_rows();
  	}
  	
  	function get_actu_categories($id,$per_page,$offset){
  		$this->db->select();
     	  $this->db->from('actu');
     	  $this->db->where('catid = ',$id);
     	  $this->db->limit($per_page,$offset);
     	  $query=$this->db->get();
     	  
    	return $query->result_array();
  	}
  	
  	function get_detail_actu($id){
  		$query = $this->db->get_where('actu', array('id'=>$id));
  	return $query->result_array();
  	}
  	//****************CONTACT*************************************
  
  		function envoyer($data){
  		 $query= $this->db->insert('contact',$data);
  		  return $query;
  	}
  	//*****************CONNEXION INSCRIPTION********************************
  	function check_connexion($compte){
  		$email=$compte['email'];
  		$pass=$compte['pass'];
  		$query = $this->db->get_where('client', array('email'=>$email,'pass'=>$pass));
  		return $query;
  	}
        function check_pass($compte){
  		$id=$compte['id'];
  		$pass=$compte['pass'];
  		$query = $this->db->get_where('client', array('id'=>$id,'pass'=>$pass));
  		return $query;
  	}
        function update_pass($data,$idclient){
  	 return $this->db->update('client',$data,array('id'=>$idclient));
  	}

  	function check_inscription($compte){
  		$email=$compte['email'];
  		$query = $this->db->get_where('client', array('email'=>$email));
  		return $query;
  		
  	}
  	
  	function envoyer_inscription($data){
  		 $query= $this->db->insert('client',$data);
  		  return $query;
  		  }
         //**********************PRO****************************************
         function check_connexion_pro($compte){
  		$ident=$compte['ident'];
  		$pass=$compte['pass'];
  		$query = $this->db->get_where('pro', array('ident'=>$ident,'pass'=>$pass));
  		return $query;
  	}
        function get_offres_pro($idpro){
            $this->db->select('pro_offer.*,pro_files.nom as pro_filesnom, pro_files.fichier as pro_filesfichier');
            $this->db->from('pro_offer');
     	  $this->db->join('pro_files', 'pro_offer.file_id = pro_files.id');
     	 $this->db->where('pro_offer.pro_id =',$idpro);
         $this->db->or_where('pro_offer.pro_id =', '0');
          $query=$this->db->get();
          return $query->result_array();
        }
        function get_dispo_pro($idpro){
            $this->db->select('pro_dispo.*,pro_files.nom as pro_filesnom, pro_files.fichier as pro_filesfichier');
            $this->db->from('pro_dispo');
     	  $this->db->join('pro_files', 'pro_dispo.file_id = pro_files.id');
     	 $this->db->where('pro_dispo.pro_id =',$idpro);
         $this->db->or_where('pro_dispo.pro_id =', '0');
          $query=$this->db->get();
          return $query->result_array();
        }
        function get_plv_pro($idpro,$catid){
          $this->db->select('pro_plv.*,pro_files.nom as pro_filesnom, pro_files.fichier as pro_filesfichier');
          $this->db->from('pro_plv');
     	  $this->db->join('pro_files', 'pro_plv.file_id = pro_files.id');
     	  if($catid!=0){
          $this->db->where('(pro_plv.pro_id ='.$idpro.' OR pro_plv.pro_id =0) AND pro_plv.plv_cat='.$catid);
          } else {
         $this->db->where('pro_plv.pro_id =',$idpro);
         $this->db->or_where('pro_plv.pro_id =', '0');
          }

         $query=$this->db->get();
          return $query->result_array();
        }
        function get_cat_plv_pro() {
           $query=$this->db->get('plv_pro_cat');
      	return $query->result_array();
        }
  	//*************************CLIENT***********************************
  	function get_client($idclient){
  		$query = $this->db->get_where('client', array('id'=>$idclient));
  		return $query->result_array();
  	}
        function update_client($data,$idclient){
             return $this->db->update('client',$data, array('id'=>$idclient));
        }
  	function count_commande($idclient){
  	
  		$this->db->select('commande.*,method_paiement.nom as methodnom');
     	  $this->db->from('commande');
     	  $this->db->join('method_paiement', 'method_paiement.id = commande.method');
     	 $this->db->where('commande.idclient =',$idclient);
     	 
     	   $query=$this->db->get();
  		 return $query->num_rows();
  	}
  	function get_commande($idclient,$per_page,$offset){
  	
  		$this->db->select('commande.*,method_paiement.nom as methodnom');
     	  $this->db->from('commande');
     	  $this->db->join('method_paiement', 'method_paiement.id = commande.method');
     	 $this->db->where('commande.idclient =',$idclient);
     	 $this->db->limit($per_page,$offset);
     	   $query=$this->db->get();
  		return $query->result_array();
  	}
  	function get_produit_commande($idclient,$id,$per_page,$offset){
  	
  		$this->db->select('animaux.*,detail_commande.quantite as quantite');
     	  $this->db->from('animaux');
     	  $this->db->join('detail_commande', 'detail_commande.idproduit = animaux.id');
     	  $this->db->join('commande', 'commande.id = detail_commande.idcommande');
     	  $this->db->join('client', 'client.id = commande.idclient');
     	 $this->db->where('commande.idclient =',$idclient);
     	 $this->db->where('detail_commande.idcommande =',$id);
     	 $this->db->limit($per_page,$offset);
     	   $query=$this->db->get();
  		return $query->result_array();
  	}
  	function count_produit_commande($idclient,$id){
  	
  		$this->db->select();
     	  $this->db->from('animaux');
     	  $this->db->join('detail_commande', 'detail_commande.idproduit = animaux.id');
     	  $this->db->join('commande', 'commande.id = detail_commande.idcommande');
     	  $this->db->join('client', 'client.id = commande.idclient');
     	 $this->db->where('commande.idclient =',$idclient);
     	 $this->db->where('detail_commande.idcommande =',$id);
     	 
     	   $query=$this->db->get();
  		return $query->result_array();
  	}
  
  function get_information($key,$page){
  	$this->db->select();
  	 $this->db->from('info');
  	  $this->db->where('page =',$page);
  	   $query=$this->db->get();
  	   return $query->row_array();  
  	   }
  	   //*********************acheter************
  	function   get_livraison(){
  		 $query=$this->db->get('livraison');
  		return $query->result_array(); 
  	}
  	function get_method(){
  		 $query=$this->db->get('method_paiement');
  		return $query->result_array(); 
  	}
        function check_promo($code){
         $this->db->select();
  	 $this->db->from('promotion');
  	  $this->db->where('code =',$code);
           $this->db->where('qty >','0');
  	   $query=$this->db->get();
  	   return $query;
        }
        function get_pct_promotion($code){
          $this->db->select();
  	 $this->db->from('promotion');
  	  $this->db->where('code =',$code);
           $this->db->where('qty >','0');
  	   $query=$this->db->get();
           $result=$query->row_array();
           return $result['pct'];
        }
  }
?>
